var OperationStat = {
};
$(function () {

    //院区
    $('#hospital').combobox({
        data:$.parseJSON($("#officeMap").val()),
        valueField:'value',
        textField:'text',
        onSelect:OperationStat.officeChange,
        onLoadSuccess: function () {
            var hospId = $('#hospitalId').val();
            if (hospId != "") {
                $(this).combobox("setValue", hospId);
                $(this).combobox('disable');
            } else {
                //加载完成后,val[0]写死设置选中第一项
                var val = $(this).combobox("getData");
                $(this).combobox("select", val[0].value);
            }
        }
    });

    //科室组
    $('#depart').combobox({
        data:$.parseJSON($("#departMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter:6,
        onSelect:OperationStat.medicalGroupChange
    });
    //病种
    $('#qcentity').combobox({
        data:$.parseJSON($("#entityMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter:6
    });

    OperationStat.initChartArea();

    //绑定顶部和中部蓝色数字的点击事件
    $(".logDetail").click(function(){
        var _this = $(this);
        var key = _this.attr("id");
        var desc = _this.parent().parent().next().children().html();//父级的上一个同级元素
        OperationStat.logDetail(key,desc);
    });
});

/**
 * //根据父级机构 查询子机构
 */
OperationStat.officeChange = function(that){
    var ajax = new $ax("/sds/common/deptListByHospId", function(data){
        $('#depart').combobox({
            data:data,
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onSelect:OperationStat.medicalGroupChange,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
                OperationStat.medicalGroupChange(this)
            }
        });
    },function(data){
        layer.msg("查询失败!" + data.responseJSON.message + "!",{icon: 2});
    });
    ajax.set({"hospID":that.value,"id":"depart"});
    ajax.start();
}

/**
 * 切换科室，查询下属医疗组
 */
OperationStat.medicalGroupChange = function(that){
    var ajax = new $ax(Sys.backendPath + "/sds/common/groupListByDeptId", function(data){
        $('#medicalGroup').combobox({
            data:data,
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        layer.msg("查询失败!" + data.responseJSON.message + "!",{icon: 2});
    });
    ajax.set({"deptId":that.value,"id":"medicalGroup"});
    ajax.start();
}

/**
 * 加载图表数据
 */
OperationStat.initChartArea = function(){
    var hospital = $('#hospital').combobox('getValue');
    var depart = $('#depart').combobox('getValue');
    var medicalGroup = $('#medicalGroup').combobox('getValue');
    var qcentity = $('#qcentity').combobox('getValue');
    var logType = $('#logType').combobox('getValue');
    /*var beginTime = $('#beginTime').val();
    var endTime = $('#endTime').val();*/
    var userName = $('#userName').val();

    //获取主页顶部和中部的数据
    var param = {};
    var ajax = new $ax(Sys.backendPath + "/sdc/operstat/logData", function (data) {
        OperationStat.writeTopData(data);
        OperationStat.drawMiddleChart(data);
    }, function (data) {
    });
    param.hospital = hospital;//院区
    param.depart = depart;//科室ID
    param.medicalGroup = medicalGroup;//医疗组
    param.qcentity = qcentity;
    param.logType = logType;
/*    param.beginTime = beginTime;
    param.endTime = endTime;*/
    param.userName = userName;
    ajax.set(param);
    ajax.start();
};

/**
 * 写主页顶部数据
 */
OperationStat.writeTopData = function(data){
    $("#todayCount").html(data.todayCount);
    $("#thisMonthCount").html(data.thisMonthCount);
    $("#thisYearCount").html(data.thisYearCount);
    $("#logTotal").html(data.logTotal);
};
/**
 * 画中部图形
 */
OperationStat.drawMiddleChart = function(data){
    var chartParams = {
        backgroundColor : '#ffffff',
        isHorizontal : false,
        legendShow : false,
        showLabel : true,
        legend:{
            show : false
        }
    };
    //日志类型 饼图
    pieCharts($("#logTypeChart"),data.logTypeChartData,"",null,chartParams);
    //日志日期趋势图
    var lines = [
        {desc:'数量',data:[],code:'logCount'}
    ];
    chartParams.grid={
        containLabel: true, //注意加上，否则axisLabel有可能显示会不全
        top:"30px",
        left:"5px",
        right:"5px",
        bottom:"5px"
    };
    lineChartVte($("#logTimeChart"), lines, data.logTimeChartData, "",null,chartParams);
    //病种分类 柱状图
    lines = [
        {desc:'数量',data:[],code:'logCount'}
    ];
    chartParams.grid={
        containLabel: true, //注意加上，否则axisLabel有可能显示会不全
        top:"30px",
        left:"5px",
        right:"5px",
        bottom:"5px"
    };
    barChartVte($("#qcentityChart"), lines, data.qcentityChartData, "",null,chartParams);
    //科室分布 柱状图
    lines = [
        {desc:'数量',data:[],code:'logCount'}
    ];
    chartParams.grid={
        containLabel: true, //注意加上，否则axisLabel有可能显示会不全
        top:"30px",
        left:"5px",
        right:"5px",
        bottom:"5px"
    };
    barChartVte($("#depatChart"), lines, data.depatChartData, "",null,chartParams);
    //用户分布 柱状图
    lines = [
        {desc:'数量',data:[],code:'logCount'}
    ];
    chartParams.grid={
        containLabel: true, //注意加上，否则axisLabel有可能显示会不全
        top:"30px",
        left:"5px",
        right:"5px",
        bottom:"5px"
    };
    barChartVte($("#userChart"), lines, data.userChartData, "",null,chartParams);
};

/**
 * 钻取主页蓝色数字详细信息
 */
OperationStat.logDetail = function(key,desc){
    var hospital = $('#hospital').combobox('getValue');
    var depart = $('#depart').combobox('getValue');
    var medicalGroup = $('#medicalGroup').combobox('getValue');
    var qcentity = $('#qcentity').combobox('getValue');
    var logType = $('#logType').combobox('getValue');
    /*var beginTime = $('#beginTime').val();
    var endTime = $('#endTime').val();*/
    var userName = $('#userName').val();

    var href = Sys.backendPath + "/sdc/sdcbusioperationlog?key="+key+"&hospital="+hospital+"&depart="+depart+"&medicalGroup="+medicalGroup+"&qcentity="+qcentity+"&logType="+logType+"&userName="+userName;

    layer.open({
        type: 2,
        title: ['<i class = "fa fa-list-ol"></i>&nbsp;'+desc,'padding-left:20px'],
        area:["95%","95%"],
        scrollbar: false,//为了禁止父页面滚动条滚动
        skin:'classfyLBStyle layer-ext-hisui',
        content: href //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
    });
    //Sys.addTabPage($.trim(desc), href,true);

};